<?php
/**
 * @since 2012.01.20.
 * @author Karasz Tamas
 * @link http://binga.hu/
 * @package Galéria mentése
 * ______ _____ _   _ _____  ___  
 * | ___ \_   _| \ | |  __ \/ _ \ 
 * | |_/ / | | |  \| | |  \/ /_\ \
 * | ___ \ | | | . ` | | __|  _  |
 * | |_/ /_| |_| |\  | |_\ \ | | |
 * \____/ \___/\_| \_/\____|_| |_/
 * 
 */
 
$admin_pre_folder = "../../";

include $admin_pre_folder.'class.php';

/*
print "<pre>";
print_r($_POST);
print_r($_FILES);
print "</pre>";
*/

if ($_POST['action']=="edit" || $_POST['action']=="add")
{
	$gallery_id = $sql->escape($_POST['id']);
	
	// gallery tábla updatelése
	$update = array(
		"gallery_name" => "'".$sql->escape($_POST['gallery_name'])."'",
		"gallery_text" => "'".$sql->escape($_POST['gallery_text'])."'",
		"gallery_facebook_text" => "'".$sql->escape($_POST['gallery_facebook_text'])."'",
		"gallery_theme" => (int)$_POST['gallery_theme']
	);
	
	// kép feltöltése
	if ($_POST['gallery_image_imagefile']<>"none")
	{
		$update['gallery_image'] = "'".$sql->escape($_POST['gallery_image_imagefile'])."'";
	}
	elseif ( $_FILES['gallery_image_imageupload']['error'] == 0 )
	{
		$folder = $admin_pre_folder.IMAGE_UPLOAD;
		$filename = strtolower($_FILES['gallery_image_imageupload']['name']);
		$filename = $sql->escape($filename);
	
		// file névben tiktott karaktere
		$search = array(
					"ő", "ö", "ó", "ü", "ú", "ű", "á", "í", " ", ","
		);
	
		// és cseréjük
		$replace = array(
					"o", "o", "o", "u", "u", "u", "a", "i", "_", "_"
		);
	
		// maga a csere fojamata
		$filename = str_replace($search,$replace,$filename);
	
		// file név ellenőrzése hogy már van-e használatban
		if ( is_file($folder.$filename) )
		{
			$filename = time().$filename;
		}
	
		// file név ellenőrzése hogy elfog-e férni az adatbázisban
		if ( strlen($filename)>100 )
		{
			$info = pathinfo($folder.$filename);
			$filename = time().".".$info['extension'];
		}
	
		// minden jó, file átmásolása, és adatbázisban elmentése
		move_uploaded_file($_FILES['gallery_image_imageupload']['tmp_name'],$folder.$filename);
		$update['gallery_image'] = "'".$filename."'";
	}
	
	if ($_POST['action']=="add")
	{
		// új elem hozzáadása
		
		$facebooktext = $_POST['gallery_text'];
		$facebooktext = str_replace("<br />", "\n", $facebooktext);
		$facebooktext = strip_tags($facebooktext);
		$facebooktext = html_entity_decode($facebooktext,ENT_COMPAT,"UTF-8");
		
		$gallery_id = $facebook->brendon_create_gallery($_POST['gallery_name'],$facebooktext);
		
		$update['gallery_id'] = $gallery_id;
		
		//$update['gallery_id'] = $gallery_id;
		
		$sql->mysql_insert("gallery",$update);
	}
	else
	{
		//sql update elvégzése
		$sql->mysql_update("gallery",$update,"gallery_id = ".$gallery_id);
		
		//Nem lehet api-ból albumot szerkeszteni
		
	}
	
	// theme exportálása css file-ba
	$sql->mysql_select("theme", "*","theme_id = ".$update['gallery_theme']);
	$theme = $sql->get_line();
	
	$file_write = "
		.gallery-ittem-".$gallery_id."{
			".($theme['theme_ittem_bg']<>""?"background-color: #".$theme['theme_ittem_bg'].";":"")."
			".($theme['theme_ittem_color']<>""?"color: #".$theme['theme_ittem_color'].";":"")."
		}
		
		.gallery-ittem-".$gallery_id." .gallery-title a,
		.upload-".$gallery_id." .upload-title,
		.image-view-".$gallery_id." .image-title{
			".($theme['theme_ittem_title']<>""?"color: #".$theme['theme_ittem_title'].";":"")."
		}
		
		.gallery-ittem-".$gallery_id." .gallery-title a:hover{
			".($theme['theme_ittem_title_hover']<>""?"color: #".$theme['theme_ittem_title_hover'].";":"")."
		}
		
		.gallery-ittem-".$gallery_id." .gallery-title a:hover{
			".($theme['theme_ittem_title_hover']<>""?"color: #".$theme['theme_ittem_title_hover'].";":"")."
		}
		
		.gallery-ittem-".$gallery_id." .button,
		.upload-".$gallery_id." .button,
		.image-view-".$gallery_id." .image-share .button{
			".($theme['theme_ittem_button_bg']<>""?"background-color: #".$theme['theme_ittem_button_bg'].";":"")."
			".($theme['theme_ittem_button_color']<>""?"color: #".$theme['theme_ittem_button_color'].";":"")."
		}
		
		.gallery-ittem-".$gallery_id." .button:hover,
		.upload-".$gallery_id." .button:hover,
		.image-view-".$gallery_id." .image-share .button:hover{
			".($theme['theme_ittem_button_bg_hover']<>""?"background-color: #".$theme['theme_ittem_button_bg_hover'].";":"")."
			".($theme['theme_ittem_button_color_hover']<>""?"color: #".$theme['theme_ittem_button_color_hover'].";":"")."
		}
		
		.imagelist-list-".$gallery_id.",
		.image-view-".$gallery_id.",
		.upload-".$gallery_id."{
			".($theme['theme_ittem_imagelist_bg']<>""?"background-color: #".$theme['theme_ittem_imagelist_bg'].";":"")."
		}
	";
	
	$myFile = $admin_pre_folder."../theme/style-".$gallery_id.".css";
	$fh = fopen($myFile, 'w');
	fwrite($fh, $file_write);
	fclose($fh);
	
	$_SESSION['message']['highlight'][] = "Módosítások sikeresen megtörténtek!";
	
	//die();
	header("Location: ".$admin_pre_folder."index.php?gallery.edit.".$gallery_id);
}


// sorrendbe rakás
if ($_POST['action']=="order")
{
	$order = 0;
	foreach ($_POST['order'] as $gallery_id) {
		$update['gallery_order'] = $order++;
		
		$sql->mysql_update("gallery", $update,"gallery_id = ".$gallery_id);
	}
	
	$_SESSION['message']['highlight'][] = "ALbumok sorrendje sikeresen meg lett változtatva!";
	header("Location: ".$admin_pre_folder."index.php?gallery.order");
}


// törlés
if ($_GET['action']=="delete")
{
	/*
	print "<pre>";
	print_R($_GET);
	print "</pre>";
	*/
	$id = $sql->escape($_GET['id']);
	
	$sql->mysql_delete("image","`image_gallery` = '".$id."'");
	$sql->mysql_delete("gallery","`gallery_id` = '".$id."'");
	
	$_SESSION['message']['highlight'][] = "Az albumot sikeresen töröltük!";
	
	//Nem lehet api-ból albumot törölni 
	//$facebook->brendon_delete_gallery($id);
	
	header("Location: ".$admin_pre_folder."index.php?gallery.list");
}

// felhasználó kitíltása
if ($_POST['action']=="bann")
{
	foreach ($_POST['deleteimage'] as $delete) {
		$sql->mysql_delete("image","image_id = ".$delete);
		$facebook->brendon_delete_image($delete);
	}
	
	foreach ($_POST['bannuser'] as $bannuser) {
		$data = array(
			"uploader_ban" => 1
		);
		$sql->mysql_update("uploader", $data,"`uploader_id` = '".$bannuser."'");
	}
}
?>